#ifndef CFMITSOCKET_H
#define CFMITSOCKET_H

#include <sys/types.h>
#ifdef _OS_LINUX_
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#endif

#ifdef _OS_WIN32_
#include <WinSock2.h>
typedef int socklen_t;
#endif
#include <string>

#include "DLLDesc.h"

const int MAXHOSTNAME = 200;
const int MAXCONNECTIONS = 5;
const int MAXRECV = 500;

namespace Polaris_CommonLib {

class _DLL_DESC_ CFMITSocket
{
 public:
  CFMITSocket();
  virtual ~CFMITSocket();

  // Server initialization
  bool create();
  bool bind ( const int port );
  bool listen() const;
  bool accept ( CFMITSocket& ) const;

  // Client initialization
  bool connect ( const std::string host, const int port );

  // Data Transimission
  bool send ( const std::string ) const;
  int recv ( std::string& ) const;
  int recv ( std::string&, int timeout_ms ) const;

  int recv( char *a_pBuf, int a_BufSize, int timeout_ms ) const;
  bool send( char *a_pBuf, int a_BufSize ) const;

  void set_non_blocking ( const bool );

  bool is_valid() const { return m_sock != -1; }

 private:

  int m_sock;
  sockaddr_in m_addr;

#ifdef _OS_WIN32_
	static bool m_IsWin32SocketInit;
#endif


};

} // End namespace

/*
How to use?
1. define macro _OS_WIN32_ or _OS_LINUX_
2. add link library : Ws2_32.lib
*/


#endif // CFMITSOCKET_H
